package com.example.back.service.impl;

import com.example.back.dto.req.ModuleReq;
import com.example.back.dto.MenuTree;
import com.example.back.dto.resp.ModuleResp;
import com.example.back.jpa.SysModuleRepository;
import com.example.back.jpa.SysRoleAdminRepository;
import com.example.back.mapper.SysModuleMapper;
import com.example.back.service.SysModuleService;
import com.example.back.utils.LevelUtils;
import com.example.back.utils.UserUtils;
import com.example.common.entity.Module;
import com.example.common.entity.RoleAdmin;
import com.example.common.support.ApiException;
import com.example.common.utils.CheckUtils;
import com.example.common.utils.IdUtils;

import org.javers.spring.annotation.JaversAuditable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class SysModuleServiceImpl implements SysModuleService {

    @Resource
    private SysRoleAdminRepository sysRoleAdminRepository;

    @Resource
    private SysModuleMapper sysModuleMapper;

    @Resource
    private UserUtils userUtils;
    @Resource
    private SysModuleRepository sysModuleRepository;


    /**
     * 获取左边菜单栏切换
     * @return
     */
    @Override
    public List<MenuTree> getMenu() {
        //查询所有角色

        List<Module> sysModuleList = new ArrayList<>();
        List<RoleAdmin> sysRoleAdmins = sysRoleAdminRepository.findByUid(userUtils.getUser().getId());
        for (RoleAdmin sysRoleAdmin : sysRoleAdmins) {
            List<Module> sysModuleList1 = sysModuleMapper.queryModByRoleId(sysRoleAdmin.getRid());
            sysModuleList.addAll(sysModuleList1);
        }
        return LevelUtils.level(sysModuleList,false);
    }

    @Override
    public List<MenuTree> list() {

        List<Module> sysModuleList = sysModuleRepository.findAll();
        return LevelUtils.level(sysModuleList,false);
    }


    /**
     * 修改和添加模块提交
     * @param req
     * @return
     */
    @Override
    @JaversAuditable
    public Object commitSave(ModuleReq req) {

        if(req.getID() ==0){

            com.example.common.entity.Module sysModule = new com.example.common.entity.Module();
            sysModule.setId(IdUtils.generateId());
            sysModule.setIcon(req.getIcon());
            sysModule.setName(req.getName());
            sysModule.setPid(req.getPID());
            sysModule.setSort(req.getSort());
            sysModule.setUrl(req.getUrl());
            sysModule.setShowUrl(req.getShowUrl());
            sysModule.setVisible(true);
            return sysModuleRepository.save(sysModule);
        }else{
            com.example.common.entity.Module obj = sysModuleRepository.findById(req.getID()).orElse(null);
            if(obj==null){
                throw new ApiException(1, "错误的请求参数");
            }
            
            obj.setIcon(req.getIcon());
            obj.setName(req.getName());
            obj.setSort(req.getSort());
            obj.setUrl(req.getUrl());
            obj.setShowUrl(req.getShowUrl());
            return sysModuleRepository.save(obj);
        }

    }

    /**
     * 删除模块
     * @param req
     * @return
     */
    @Override
    @JaversAuditable
    public Object commitDel(ModuleReq req) {

        Module sysModule = sysModuleRepository.findById(req.getID()).orElse(null);

        if(sysModule == null){
            throw new ApiException(1, "错误的请求参数");
        }
        sysModuleRepository.deleteById(req.getID());

        return null;
    }

    /**
     *禁用启用模块
     * @param req
     * @return
     */
    @Override
    @JaversAuditable
    public Object updateVisible(ModuleReq req) {
        Module sysModule = sysModuleRepository.findById(req.getID()).orElse(null);
        if(sysModule == null){
            CheckUtils.checkNull(req.getID(),new ApiException(1, "错误的请求参数"));
        }
        sysModule.setVisible(!sysModule.getVisible());

        return sysModuleRepository.save(sysModule);
    }

    @Override
    public Object EditModuleCate(ModuleReq req) {

        Module sysModule = sysModuleRepository.findById(req.getID()).orElse(null);
        if(sysModule==null){
            throw new ApiException(1, "错误的请求参数");
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("ID", sysModule.getId());
        hashMap.put("Name", sysModule.getName());
        hashMap.put("Icon", sysModule.getIcon());
        hashMap.put("ShowUrl", sysModule.getShowUrl());
        hashMap.put("Url", sysModule.getUrl());
        hashMap.put("Sort", sysModule.getSort());
        hashMap.put("PID", sysModule.getPid());

        return hashMap;
    }

}
